<?php

namespace App\Admin\Controllers\Posts;

use App\Admin\Actions\Grid\BatchCommentAction;
use App\Admin\Actions\Grid\CommentAction;
use App\Admin\Actions\Grid\Posts\CommentApplyAction;
use App\Admin\Repositories\Posts\WxComment;
use App\Jobs\Posts\UpdateCommentOnlyTextJob;
use App\Wen\Utils\Settings;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxCommentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxComment(), function (Grid $grid) {
            global $__MINI_GLOBAL_TENANT_ID__;
            if($__MINI_GLOBAL_TENANT_ID__ > 0){
                $grid->model()->whereHas('post', function ($query) use ($__MINI_GLOBAL_TENANT_ID__){
                    $query->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__);
                });
            }

            $grid->simplePaginate();
            $grid->quickSearch(['comment_content','id','user_name', 'comment_agent_name'])->placeholder('搜索评论内容，ID，评论人的名字，被评论的人名字');
            $grid->model()->orderBy('id','desc');
            $grid->column('id')->sortable();
            $grid->column('posts_id');
//            $grid->column('posts_user_id');
            $grid->column('users', '头像')->display(function ($v) {
                return '<img src="' . ($this->user['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('user')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');
            $grid->column('comment_agent_avatar')->image('',50);
            $grid->column('comment_agent_name');
            $grid->column('comment_content')->display(function ($v){
                return _mini_phone(_mini_emoji(_mini_aite_replace($v, true), true), true);
            })->limit(20);
            $grid->column('comment_img_url')->image(100);
            $grid->column('is_anonymous')->display(function ($v){
                if($v == 1){
                    return '是';
                }else{
                    return '';
                }
            });
            $grid->column('comment_state')->using([0=>'待审核',1=>'审核通过',2=>'驳回'])->label([
                0=>'red',
                1=>'green',
                2=>'default'
            ])->sortable();
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

            $grid->batchActions(function ($batch) {
                $batch->add((new BatchCommentAction()));

            });

            global $__MINI_GLOBAL_TENANT_ID__;
            if($__MINI_GLOBAL_TENANT_ID__ === 0){
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->append((new CommentAction())->setKey($this->id));
                    $actions->append((new CommentApplyAction())->setKey($this->id));
                });
            }else{
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->append((new CommentAction())->setKey($this->id));
                });
            }

            $grid->disableCreateButton();
            // 禁用编辑按钮
//            $grid->disableEditButton();
            // 禁用详情按钮
            $grid->disableViewButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxComment(), function (Show $show) {
            $show->field('id');
            $show->field('posts_id');
            $show->field('posts_user_id');
            $show->field('user_id');
            $show->field('user_name');
            $show->field('user_avatar');
            $show->field('comment_agent_id');
            $show->field('comment_agent_name');
            $show->field('comment_agent_avatar');
            $show->field('comment_content');
            $show->field('comment_id');
            $show->field('is_anonymous');
            $show->field('comment_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxComment(), function (Form $form) {
            $form->display('id');
//            $form->text('posts_id');
//            $form->text('posts_user_id');
//            $form->text('user_id');
//            $form->text('user_name');
//            $form->image('user_avatar')->url('files/uploads')->autoUpload();
//            $form->text('comment_agent_id');
//            $form->text('comment_agent_name');
//            $form->text('comment_agent_avatar');
            $form->textarea('comment_content');
            $form->image('comment_img_url')->url('files/uploads')->autoUpload();
//            $form->text('comment_id');
            $form->radio('comment_state', '状态')->options([0=>'待审核',1=>'审核通过',2=>'驳回']);
            $form->saved(function (Form $form){
                \App\Models\Posts\WxComment::where('id', $form->model()->id )->update(['is_only_text'=>null]);
                UpdateCommentOnlyTextJob::dispatch($form->model()->id);
            });

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    if(\App\Models\Posts\WxComment::where('id', $form->getKey())->value('tenant_id') != $__MINI_GLOBAL_TENANT_ID__){
                        return $form->response()->error('权限不足，不可以删除其他分站对象');
                    }
                }
            });

        });
    }
}
